package it.csi.finregio.finreges.presentation.action;

import it.crp.crparch.web.struts.plugin.CRPStrutsPlugin;
import it.crp.crparch.web.struts.service.CRPStrutsService;
import it.crp.logger.PrdLoggerFactory;
import it.csi.finregio.finreges.presentation.streaminfo.AbstractStreamInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DownloadAction;


public class FinregioDownloadAction extends DownloadAction{

	private Logger log = PrdLoggerFactory.getInstance().getLogger("finreges");

	protected StreamInfo getStreamInfo(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		log .info("[FinregioDownloadAction::getStreamInfo] BEGIN");
		CRPStrutsPlugin plugin = (CRPStrutsPlugin) getServlet().getServletContext().getAttribute(CRPStrutsPlugin.PLUGIN_NAME);
		CRPStrutsService service = (CRPStrutsService) plugin.getService(mapping.getPath());
		AbstractStreamInfo streamInfo =  (AbstractStreamInfo) service.execute(form);
		response.addHeader("Content-Disposition", "attachment;filename=\""+streamInfo.getFileName()+"\"");

		return streamInfo;
	}

}
